// pages/classify/classify.js
const {
  homeCategory,
  categoryId,
  addCart,
  goodsDetail
} = require('../../utils/http')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeKey: 0,
    categoryClassifyList: [],
    classifyId: 0,
    //左侧点击样式
    activeId: 0,
    // 右侧数据
    goodsList: [],
    show:false,
    popupInfo:{}
  },
  showPopup(e){
    const that = this
    const id = e.currentTarget.dataset.operation
    goodsDetail(id).then(res=>{
      console.log(res)
      that.setData({
        show:true,
        popupInfo:res.data
      })
    })
  },
  onChange(event) {
    console.log(event.detail);
  },
  onClose(){
    this.setData({
      show:false
    })
  },
  toList(e) {
    const id = e.currentTarget.dataset.operation
    this.getRightList(id)
  },
  toDetail(e){
    // const id = e.currentTarget.dataset.operation
    // goodsDetail(id).then(res=>{
    //   console.log(res)
    // })
    console.log(123)
    // wx.navigateBack({
    //   url: 'pages/goodsDetail/goodsDetail'
    // })
  },
  getRightList(id) {
    categoryId(id).then(res => {
      if (res.msg == 'success') {
        this.setData({
          activeId: id,
          goodsList: res.data
        })
        console.log(this.data.goodsList)
      } else {
        this.setData({
          activeId: id,
          goodsList: []
        })
        console.log('没有数据')
      }
    })
  },
  toGoodsDetail(e){
    const id = e.currentTarget.dataset.operation
    console.log(id)
    // url="/pages/goodsDetail/goodsDetail?id={{item.id}}"
    wx.navigateTo({
      url: `/pages/goodsDetail/goodsDetail?id=${id}`
    })
  },
  addToCar(e){
    const id = e.currentTarget.dataset.operation
    const token = wx.getStorageSync('token')
    const toCarInfo = {}
    toCarInfo.goodsId = id
    toCarInfo.number = 1
    toCarInfo.token = token
    addCart(toCarInfo).then(res=>{
        console.log(res)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    homeCategory().then(res => {
      // console.log(res.data[0].id)
      this.getRightList(res.data[0].id)
      this.setData({
        categoryClassifyList: res.data
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})